class Solution(object):
    def nextGreaterElement(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        n = len(nums2)
        stack = []
        res = [-1] * n
        i = 0
        while i < n:
            if len(stack) == 0 or nums2[i] <= nums2[stack[-1]]:
                stack.append(i)
                i += 1
            else:
                i_ = stack.pop()
                res[i_] = nums2[i]
        return [res[nums2.index(r)] for r in nums1]
